﻿using Microsoft.AspNetCore.Mvc;
using S3_MVC_UserManager.Models;
using System.Data;
using System.Diagnostics;
using System.Reflection;

namespace S3_MVC_UserManager.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        static DataTable userTable;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;

            if (userTable == null)
            {
                userTable = new DataTable("user");
                userTable.Columns.Add("id");
                userTable.Columns.Add("name");
                userTable.Columns.Add("gender");
                userTable.Columns.Add("age");
            }

        }
        /// <summary>
        /// 用户列表页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            //DataTable userTable = new DataTable("user");
            //userTable.Columns.Add("id");
            //userTable.Columns.Add("name");
            //userTable.Columns.Add("gender");
            //userTable.Columns.Add("age");
            this.ViewData["userTable"] = userTable;

            return View();
        }
        [HttpGet]
        public IActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public IActionResult DoAdd([FromBody] UserModel model)
        {
            userTable.Rows.Add(model.Id, model.Name, model.Gender, model.Age);

            return Json(new
            {
                Message = "保存成功"
            });
        }
        [HttpGet]
        public IActionResult Edit(string id)
        {
            DataRow row = GetUser(id);
            if (row != null)
            {
                UserModel model = new UserModel()
                {
                    Id = row["id"].ToString(),
                    Name = row["name"].ToString(),
                    Gender = row["gender"].ToString(),
                    Age = row["age"].ToString()
                };
                this.ViewData["user"] = model;
            }
            
            return View();
        }

        [HttpPost]
        public IActionResult DoEdit([FromBody] UserModel model)
        {
            DataRow row = GetUser(model.Id);
            if (row != null)
            {
                row["name"] = model.Name;
                row["gender"] = model.Gender;
                row["age"] = model.Age;
            }
            return Json(new
            {
                Message = "更新成功"
            });
        }

        [HttpPost]
        public IActionResult DoDelete(string id)
        {
            DataRow row = GetUser(id);
            if (row != null)
            {
                row.Delete();
            }
            return Json(new
            {
                Message = "删除成功"
            });
        }

        private DataRow GetUser(string id)
        {
            for (int i = 0; i < userTable.Rows.Count; i++)
            {
                DataRow row = userTable.Rows[i];
                if (row["id"].ToString() == id)
                {
                    return row;
                }
            }
            return null;
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
